﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Xml.Utility;

namespace Portal.Cms.Model
{
    public class RelationTypeCls
    {
        public string RelationTypeId;
        public string RelationTypeName;
        public int    SortIndex=0;
        public int InOut = 0;
        public string frkGroupDataId;
    }

    public class RelationTypeParser
    {
        public static RelationTypeCls CreateInstance()
        {
            //tao 1 class trang....
            RelationTypeCls oRelationTypeCls = new RelationTypeCls();
            return oRelationTypeCls;
        }

        public static RelationTypeCls ParseFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            RelationTypeCls oRelationTypeCls = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oRelationTypeCls;
        }


        public static RelationTypeCls ParseFromDataRow(DataRow dr)
        {
            RelationTypeCls oRelationTypeCls = new RelationTypeCls();
            oRelationTypeCls.RelationTypeId = XmlUtility.GetString(dr, "RelationTypeId", true);
            oRelationTypeCls.frkGroupDataId = XmlUtility.GetString(dr, "frkGroupDataId", true);
            oRelationTypeCls.RelationTypeName = XmlUtility.GetString(dr, "RelationTypeName", true);
            oRelationTypeCls.SortIndex = XmlUtility.GetInt(dr, "SortIndex", true);
            oRelationTypeCls.InOut = XmlUtility.GetInt(dr, "InOut", true);
            return oRelationTypeCls;
        }

        public static RelationTypeCls[] ParseFromDataTable(DataTable dtTable)
        {
            RelationTypeCls[] RelationTypes = new RelationTypeCls[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                RelationTypes[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return RelationTypes;
        }

        public static DataSet CreateDataset()
        {
            DataSet ds = new DataSet();
            ds.Tables.Add("Info");

            ds.Tables["Info"].Columns.Add("RelationTypeId");
            ds.Tables["Info"].Columns.Add("frkGroupDataId");
            ds.Tables["Info"].Columns.Add("RelationTypeName");
            ds.Tables["Info"].Columns.Add("SortIndex");
            ds.Tables["Info"].Columns.Add("InOut");
            return ds;
        }

        public static string GetXml(RelationTypeCls oRelationTypeCls)
        {
            DataSet ds = CreateDataset();
            ds.Tables["Info"].Rows.Add(new object[]{
                oRelationTypeCls.RelationTypeId,
                oRelationTypeCls.frkGroupDataId,
                oRelationTypeCls.RelationTypeName,
                oRelationTypeCls.SortIndex,
                oRelationTypeCls.InOut
            });
            return ds.GetXml();
        }

        public static string GetXml(DataRow dr)
        {
            RelationTypeCls oRelationType = RelationTypeParser.ParseFromDataRow(dr);
            string Xml = RelationTypeParser.GetXml(oRelationType);
            return Xml;
        }

        public static string GetXml(DataTable dt)
        {
            if (dt == null) return null;
            RelationTypeCls[] RelationTypes = new RelationTypeCls[dt.Rows.Count];
            for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
            {
                RelationTypes[iIndex] = RelationTypeParser.ParseFromDataRow(dt.Rows[iIndex]);
            }
            string Xml = RelationTypeParser.GetXml(RelationTypes);
            return Xml;
        }


        public static string GetXml(RelationTypeCls[] RelationTypes)
        {
            if (RelationTypes == null)
            {
                //neu null thi khoi tao = mang rong de cho do loi...!
                RelationTypes = new RelationTypeCls[0];
            }
            DataSet ds = CreateDataset();
            for (int iIndex = 0; iIndex < RelationTypes.Length; iIndex++)
            {
                RelationTypeCls oRelationTypeCls = RelationTypes[iIndex];
                ds.Tables["Info"].Rows.Add(new object[]
                {
                    oRelationTypeCls.RelationTypeId,
                    oRelationTypeCls.frkGroupDataId,
                    oRelationTypeCls.RelationTypeName,
                    oRelationTypeCls.SortIndex,
                    oRelationTypeCls.InOut
                });
            }
            string XmlData = ds.GetXml();
            return XmlData;
        }

        public static RelationTypeCls[] ParseMultiRecordFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if (ds.Tables.Count == 0)
            {
                return new RelationTypeCls[0];
            }
            RelationTypeCls[] RelationTypes = new RelationTypeCls[ds.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
            {
                RelationTypes[iIndex] = ParseFromDataRow(ds.Tables[0].Rows[iIndex]);
            }
            return RelationTypes;
        }
    }
}
